<?php

declare(strict_types=1);

namespace App\Controller\AdminApi;

use Dotenv\Validator;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;
use App\Exception\XZXValidationException;
use App\Service\RateBocService;
use App\Utils\CustomUtils;
use App\Utils\DateUtils;
use Psr\Container\ContainerInterface;
use Hyperf\Logger\LoggerFactory;
use App\Middleware\AdminApi\AdminJWTAuthMiddleware;

/**
 * Class RateBocController
 * @package App\Controller
 *
 * @Controller(prefix="/admin/api/rateBoc")
 */
class RateBocController extends AdminBaseController
{
    /**
     * @Inject()
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;

    /**
     * @Inject()
     * @var RateBocService
     */
    protected $RateBocService;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    public function __construct(LoggerFactory $loggerFactory)
    {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    // /**
    //  * 查看
    //  * @PostMapping(path = "get")
    //  */
    // public function get(RequestInterface $request)
    // {
    //     $validator = $this->validationFactory->make(
    //         $request->all(),
    //         [
    //             'id' => 'required',
    //         ],
    //     );

    //     if ($validator->fails()){
    //         // Handle exception
    //         $errorMessage = $validator->errors()->first();  
    //         throw new XZXValidationException($errorMessage);
    //     }

    //     $param = $request->all();
    //     return $this->RateBocService->get($param);
    // }

    // /**
    //  * 编辑
    //  * @PostMapping(path = "edit")
    //  */
    // public function edit(RequestInterface $request)
    // {

    //     $validator = $this->validationFactory->make(
    //         $request->all(),
    //         [
    //             'id' => 'required',
    //         ],
    //     );

    //     if ($validator->fails()){
    //         // Handle exception
    //         $errorMessage = $validator->errors()->first();  
    //         throw new XZXValidationException($errorMessage);
    //     }

    //     $param = $request->all();
    //     return $this->RateBocService->edit($param);
    // }

    // /**
    //  * 添加
    //  * @PostMapping(path = "add")
    //  */
    // public function add(RequestInterface $request)
    // {

    //     $validator = $this->validationFactory->make(
    //         $request->all(),
    //         [
    //             //'desciption' => 'required',
    //             //'name' => 'required',
    //         ],
    //     );

    //     if ($validator->fails()){
    //         // Handle exception
    //         $errorMessage = $validator->errors()->first();  
    //         throw new XZXValidationException($errorMessage);
    //     }

    //     $param = $request->all();
    //     return $this->RateBocService->add($param);
    // }

    // /**
    //  * 删除
    //  * @PostMapping(path = "del")
    //  */
    // public function del(RequestInterface $request)
    // {

    //     $validator = $this->validationFactory->make(
    //         $request->all(),
    //         [
    //             'id' => 'required',
    //         ],
    //     );

    //     if ($validator->fails()){
    //         // Handle exception
    //         $errorMessage = $validator->errors()->first();  
    //         throw new XZXValidationException($errorMessage);
    //     }

    //     $param = $request->all();
    //     return $this->RateBocService->del($param);
    // }

    /**
     * 查询
     * @PostMapping(path = "list")
     * @Middleware(AdminJWTAuthMiddleware::class)
     */
    public function list(RequestInterface $request)
    {

        $validator = $this->validationFactory->make(
            $request->all(),
            [
                'page' => 'required|integer|min:1',
                'limit' => 'required|integer|min:1',
                'search.currency' => 'sometimes|required|alpha',
            ]
        );

        if ($validator->fails()){
            // Handle exception
            $errorMessage = $validator->errors()->first();  
            throw new XZXValidationException($errorMessage);
        }
        
        $param = $request->all();

        if(isset($param['search'])){
            $search = $param['search'];
            CustomUtils::xzxAddSearch($param, $search, 'currency');
            CustomUtils::xzxAddSearch($param, $search, 'createTimeRange');
            if(isset($param["createTimeRange"]))
            {
                try {
                    $param['createTimeRange'][0] = DateUtils::dateToTimeStamp($param['createTimeRange'][0]);
                    $param['createTimeRange'][1] = DateUtils::dateToTimeStamp($param['createTimeRange'][1]);
                } catch (\Throwable $th) {
                    unset($param['createTimeRange']);
                }
            }
    
        }

        
        return $this->RateBocService->list($param);
    }

    /**
     * 查询汇率
     * @PostMapping(path = "getrate")
     * @Middleware(AdminJWTAuthMiddleware::class)
     */
    public function getRate(RequestInterface $request) {
        
        // $param = array();
        // $param['srcCurrency'] = trim($request->post('srcCurrency'));
        // $param['destCurrency'] = trim($request->post('destCurrency'));
        // $param['type'] = trim($request->post('type',5));

        $validator = $this->validationFactory->make(
            $request->all(),
            [
                'srcCurrency' => 'required|alpha',
                'destCurrency' => 'required|alpha',
                'type' => 'required|digits_between:1,5',
            ]
        );

        if ($validator->fails()){
            // Handle exception
            $errorMessage = $validator->errors()->first();  
            throw new XZXValidationException($errorMessage);
        }
        $param = $request->all();
        CustomUtils::xzxDebug($param);
        
        return $this->RateBocService->getRate($param);        
    }

    /**
     * 查询历史记录
     * @PostMapping(path = "loglist")
     * @Middleware(AdminJWTAuthMiddleware::class)
     */   
    public function logList(RequestInterface $request) {
        
        // $param = array();
        // $param['page'] = trim($request->post('page',1));
        // $param['limit'] = trim($request->post('limit',1));
        // $param['rateBocId'] = trim($request->post('rateBocId'));

        // xeDebug($param);
        
        // $result = $this->RateBocService->logList($param);
        
        // return xeResponseData($result['code'],$result['message'],$result['data']);

        $validator = $this->validationFactory->make(
            $request->all(),
            [
                'page' => 'required|integer|min:1',
                'limit' => 'required|integer|min:1',
                'search.rateBocId' => 'sometimes|required|uuid',
            ]
        );

        if ($validator->fails()){
            // Handle exception
            $errorMessage = $validator->errors()->first();  
            throw new XZXValidationException($errorMessage);
        }
        
        $param = $request->all();
        $search = $param['search'];
        CustomUtils::xzxAddSearch($param, $search, 'rateBocId');
        CustomUtils::xzxAddSearch($param, $search, 'createTimeRange');
        if(isset($param["createTimeRange"]))
        {
            try {
                $param['createTimeRange'][0] = DateUtils::dateToTimeStamp($param['createTimeRange'][0]);
                $param['createTimeRange'][1] = DateUtils::dateToTimeStamp($param['createTimeRange'][1]);
            } catch (\Throwable $th) {
                unset($param['createTimeRange']);
            }
        }
        return $this->RateBocService->logList($param);
    }
}